﻿package CutLoad.components
{
	import CutLoad.Base;
	import CutLoad.cookies.Painter;
	import CutLoad.cookies.RandNO;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	/**
	 * 文本框 ,对flash.text.TextField进行了一些功能扩展
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CTextField extends Base
	{
		protected var txt:TextField
		protected var spGuide:Sprite;
		public function CTextField()
		{
		}	
		/**
		 * 
		 * 获得一些预设地TextFormat
		 * @param align 对齐方式：0左,1中,2右
		 * @param color 字色
		 * @param size 字尺寸
		 * @return 
		 * 
		 */		
		public static function getTextFormat(align:uint,color:Number,size:Number):TextFormat{
			var fmtTem:TextFormat = new TextFormat();
			if(align==0){
				fmtTem.align = TextFormatAlign.LEFT;
			}else if(align==1){
				fmtTem.align = TextFormatAlign.CENTER;
			}else if(align==2){
				fmtTem.align = TextFormatAlign.RIGHT;
			}
			fmtTem.color = color;
			fmtTem.size = size;	
			return fmtTem
		}
		/**
		 * 快速获得一个显示文本的简单的文本框 
		 * @param label 文本内容
		 * @param fmt TextFormat 默认为null
		 * @param selectable 是否可以选重 默认为false
		 * @return 
		 * 
		 */		
		public static function getSimpleTxt(label:String="",fmt:TextFormat=null,selectable:Boolean=false):CTextField{
			var txt:CTextField=new CTextField();
			txt.init(label,fmt,selectable);
			return txt;
		}
		/**
		 * 
		 * @param str 文字信息
		 * @param fmt 文本的TextFormat
		 * @param selectable 是否可以选重。
		 * 
		 */		
		public function init(str:String="",fmt:TextFormat=null,selectable:Boolean=false):void{
			txt=new TextField();
			this.setAutoSize(1);
			txt.selectable=selectable;
			this.addChild(txt);
			if(fmt==null){
				fmt = new TextFormat();
				fmt.align = TextFormatAlign.LEFT;
				fmt.color = 0x000000;
				fmt.size = 12;
			}
			setTextFormat(fmt)
			//
			this.text=str;
		}
		/**
		 * 显示文本边框
		 * 
		 */		
		public function showBodor(isShow:Boolean=true,borderColor:Number=0x0000ff):void{
			txt.border=isShow;
			txt.borderColor=borderColor;
		}
		/**
		 * 获得文本框
		 * @return 
		 * 
		 */		
		public function getTextField():TextField{
			return txt
		}
		/**
		 * 设置方本格式
		 * @param fmt
		 * @param isUpdateText 是否更新text
		 * @return 
		 * 
		 */		
		public function setTextFormat(fmt:TextFormat,isUpdateText:Boolean=true):CTextField{
			txt.defaultTextFormat=fmt;
			if(isUpdateText) txt.text=txt.text;
			return this;
		}
		/**
		 * 显示附助线
		 * 
		 */		
		public function showGuide():void{
			if(spGuide!=null) this.removeChild(spGuide);
			spGuide=Painter.getRect(this.getShowWidth(),this.getShowHeight(),0x000000,0,0,0,2,RandNO.getRndColor32());
			spGuide.mouseChildren=false;
			spGuide.mouseEnabled=false;
			spGuide.buttonMode=false;
			this.addChild(spGuide);
		}
		/**
		 * 设置背景 
		 * @param color
		 * @param w
		 * @param h
		 * 
		 */		
		public function setBackground(backColor:Number,backColorAlpha:Number=1,lineThickness:Number=0,lineColor:Number=0xff0000,lineAlpha:Number=1,w:Number=0,h:Number=0):void{
			if(spGuide!=null) this.removeChild(spGuide);
			if(w==0) w=this.getShowWidth();
			if(h==0) h=this.getShowHeight();
			spGuide=Painter.getRect(w,h,backColor,backColorAlpha,0,0,2,lineColor);
			spGuide.mouseChildren=false;
			spGuide.mouseEnabled=false;
			spGuide.buttonMode=false;
			this.addChild(spGuide);
		}
		/**
		 * 设置自动对齐的方式，默认是不自动对齐 
		 * @param t 0 none,1 left,2 center,3 right
		 * 
		 */		
		public function setAutoSize(t:Number=0):void{
			switch(t){
				case 0:
					txt.autoSize=TextFieldAutoSize.NONE;
					break;
				case 1:
					txt.autoSize=TextFieldAutoSize.LEFT
					break;
				case 2:
					txt.autoSize=TextFieldAutoSize.CENTER;
					break;
				case 3:
					txt.autoSize=TextFieldAutoSize.RIGHT;
					break;
				default:
					throw new Error("不包括范围的值");
					break;
			}
		}
		/**
		 * 文本内容
		 * @param str
		 * 
		 */		
		public function set text(str:String):void{
			txt.text=str;
		}
		/**
		 * 文本内容
		 * @return 
		 * 
		 */		
		public function get text():String{
			return txt.text;
		}
		/**
		 * html文本内容
		 * @param str
		 * 
		 */		
		public function set htmlText(str:String):void{
			txt.htmlText=str;
		}
		/**
		 * 获得字宽 
		 * @return 
		 * 
		 */		
		public function getTxtWidth():Number{
			return txt.textWidth;
		}
		/**
		 * 获得字高 
		 * @return 
		 * 
		 */		
		public function getTxtHeight():Number{
			return txt.textHeight;
		}
		//覆盖------------------------------------------------------------
		/**
		 * 设置文字的宽度
		 * @param w
		 * @param h 设置高无效
		 * 
		 */		
		override public function setWH(w:Number=0, h:Number=0):void{
			if(w!=0) txt.width=w;
			if(h!=0) txt.height=h
			//trace("CTextField setWH w:"+w+" h:"+h);
		}	
	}
}